# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
# from jms.dm.dm_whole_decide_duty_base_dt import jms_dm__dm_whole_decide_duty_base_dt

label = f"dm_whole_decide_duty_base_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dm__dm_whole_decide_duty_base_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_whole_decide_duty_base_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_whole_decide_duty_base_dt PARTITION (
         p{{{{ execution_date | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_base_dt/dt={{{{ execution_date | cst_ds }}}}/*")
        INTO TABLE dm_whole_decide_duty_base_dt
        FORMAT AS 'PARQUET'(
            sign_type
           ,cainiao_plan_sign_date
           ,ziyou_plan_sign_date
           ,order_source_code
           ,start_network_code
           ,end_network_code
           ,start_agent_code
           ,end_agent_code
           ,start_provider_code
           ,end_provider_code
           ,waybill_no
           ,platform_effect
           ,ziyou_platform_effect
           ,if_platform_standard
           ,if_doudi_platform_standard
           ,cainiao_platform_effect
           ,if_cainiao_platform_pres_standard
           ,order_source_name
           ,duty_type
           ,duty_main_code
           ,duty_main_name
           ,duty_main_type
           ,duty_main_agent_code
           ,duty_main_agent_name
           ,duty_provider_code
           ,duty_provider_desc
           ,static_scan_time
           ,dynamic_scan_time
           ,duty_shipment_no
           ,duty_shipment_name
           ,start_network_name
           ,start_city_code
           ,start_city_name
           ,start_provider_name
           ,start_fran_code
           ,start_fran_name
           ,start_agent_name
           ,start_center_code
           ,start_center_name
           ,end_network_name
           ,end_city_code
           ,end_city_name
           ,end_provider_name
           ,end_fran_code
           ,end_fran_name
           ,end_agent_name
           ,end_center_code
           ,end_center_name
           ,if_jingtai_lyqs
           ,if_dizhi_jxcw
           ,if_jingtai_my
           ,if_jiedanhou_ls
           ,if_jiaojian_jbcw
           ,if_cuojiao
           ,if_jiaojian_bzd
           ,if_zhongzhuan_jbcw
           ,if_cuofa
           ,if_fengche_wd
           ,if_caozuo_yw
           ,if_fache_wd
           ,if_yunshu_wd
           ,if_jingang_zybjs
           ,if_zhongzhuan_cf
           ,if_sanhuo_bjs
           ,if_paijian_cf
           ,if_qianshou_wd
           ,duty_main_fran_code
           ,duty_main_fran_name
        )
        columns from path as (dt)
        set(
            sign_type                              =  sign_type
           ,cainiao_plan_sign_date                 = cainiao_plan_sign_date
           ,ziyou_plan_sign_date                   = ziyou_plan_sign_date
           ,order_source_code                      = order_source_code
           ,start_network_code                     = start_network_code
           ,end_network_code                       = end_network_code
           ,start_agent_code                       = start_agent_code
           ,end_agent_code                         = end_agent_code
           ,start_provider_code                    = start_provider_code
           ,end_provider_code                      = end_provider_code
           ,waybill_no                             = waybill_no
           ,platform_effect                        = platform_effect
           ,ziyou_platform_effect                  = ziyou_platform_effect
           ,if_platform_standard                   = if_platform_standard
           ,if_doudi_platform_standard             = if_doudi_platform_standard
           ,cainiao_platform_effect                = cainiao_platform_effect
           ,if_cainiao_platform_pres_standard      = if_cainiao_platform_pres_standard
           ,order_source_name                      = order_source_name
           ,duty_type                              = duty_type
           ,duty_main_code                         = duty_main_code
           ,duty_main_name                         = duty_main_name
           ,duty_main_type                         = duty_main_type
           ,duty_main_agent_code                   = duty_main_agent_code
           ,duty_main_agent_name                   = duty_main_agent_name
           ,duty_provider_code                     = duty_provider_code
           ,duty_provider_desc                     = duty_provider_desc
           ,static_scan_time                       = static_scan_time
           ,dynamic_scan_time                      = dynamic_scan_time
           ,duty_shipment_no                       = duty_shipment_no
           ,duty_shipment_name                     = duty_shipment_name
           ,start_network_name                     = start_network_name
           ,start_city_code                        = start_city_code
           ,start_city_name                        = start_city_name
           ,start_provider_name                    = start_provider_name
           ,start_fran_code                        = start_fran_code
           ,start_fran_name                        = start_fran_name
           ,start_agent_name                       = start_agent_name
           ,start_center_code                      = start_center_code
           ,start_center_name                      = start_center_name
           ,end_network_name                       = end_network_name
           ,end_city_code                          = end_city_code
           ,end_city_name                          = end_city_name
           ,end_provider_name                      = end_provider_name
           ,end_fran_code                          = end_fran_code
           ,end_fran_name                          = end_fran_name
           ,end_agent_name                         = end_agent_name
           ,end_center_code                        = end_center_code
           ,end_center_name                        = end_center_name
           ,if_jingtai_lyqs                        = if_jingtai_lyqs
           ,if_dizhi_jxcw                          = if_dizhi_jxcw
           ,if_jingtai_my                          = if_jingtai_my
           ,if_jiedanhou_ls                        = if_jiedanhou_ls
           ,if_jiaojian_jbcw                       = if_jiaojian_jbcw
           ,if_cuojiao                             = if_cuojiao
           ,if_jiaojian_bzd                        = if_jiaojian_bzd
           ,if_zhongzhuan_jbcw                     = if_zhongzhuan_jbcw
           ,if_cuofa                               = if_cuofa
           ,if_fengche_wd                          = if_fengche_wd
           ,if_caozuo_yw                           = if_caozuo_yw
           ,if_fache_wd                            = if_fache_wd
           ,if_yunshu_wd                           = if_yunshu_wd
           ,if_jingang_zybjs                       = if_jingang_zybjs
           ,if_zhongzhuan_cf                       = if_zhongzhuan_cf
           ,if_sanhuo_bjs                          = if_sanhuo_bjs
           ,if_paijian_cf                          = if_paijian_cf
           ,if_qianshou_wd                         = if_qianshou_wd
           ,duty_main_fran_code                    = duty_main_fran_code
           ,duty_main_fran_name                    = duty_main_fran_name
           ,dt                                     = dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dm__dm_whole_decide_duty_base_dt << jms_dm__dm_whole_decide_duty_base_dt
